package CrowdCounting.RunnableTask.CrowdCountingTask.CCShowLabelTaskImp;

import Const.State;
import CrowdCounting.Model.CrowdCounting.CrowdCountingDisplayFrame;
import CrowdCounting.Model.CrowdCounting.CrowdCountingShow;
import CrowdCounting.Model.Util;
import CrowdCounting.RunnableTask.CrowdCountingTask.CCShowLabelTask;

import javax.swing.*;
import java.io.File;

public class CCShowImageLabelTask extends CCShowLabelTask {
    private String imagePathRoot;
    private int imageLength;

    public CCShowImageLabelTask(CrowdCountingDisplayFrame crowdCountingDisplayFrame, JLabel imageLabel, String imagePathRoot) {
        this.crowdCountingDisplayFrame = crowdCountingDisplayFrame;
        this.label = imageLabel;
        this.imagePathRoot = imagePathRoot;
        imageLength = Integer.valueOf(Util.loadProperties("config.properties").getProperty("imageMaxNum"));
    }

    @Override
    public void showLabel() {
        for (int index = 1; index <= imageLength; index++) {
            String imagePath = imagePathRoot + "\\image" + index + ".png";
            File imageFile = crowdCountingDisplayFrame.displayImage(this.label, imagePath);
            if (imageFile != null && !imageFile.delete())
                System.out.println("文件删除失败：" + imagePath);
            while (CrowdCountingShow.getShowLabelThreadState() == State.STOP) {
                System.out.println("暂停中");
                Util.sleepTime(1000);
            }
            if (CrowdCountingShow.getShowLabelThreadState() == State.CLOSE) break;
        }
        Util.deleteDirectory(new File(imagePathRoot));
    }

}
